<script type="text/javascript">
var paginationTotal = 1;// 记录总数
$(function() {
			initData(0); // 初始化分页<{$lang.data}>

			$("#edit").dialog({
						autoOpen : false,
						width : 350,
						height : 200,
						modal : true,
						show : "slide",
						zIndex : 100
						
					});
					
			$(".messageDiv").dialog({
						autoOpen : false,
						width : 400,
						height : 200,
						modal : true,
						show : "slide",
						zIndex : 200,
						title : "<{$lang.tip}><{$lang.msg}>",
						buttons : {
							"<{$lang.confirm}>" : function() {
								$(this).dialog('close');
							},
							"<{$lang.return}>" : function() {
								$(this).dialog('close');
							}
						}
					});

			$("#confrimDiv").dialog({
						autoOpen : false,
						width : 400,
						height : 200,
						modal : true,
						show : "slide",
						zIndex : 200,
						title : "<{$lang.tip}><{$lang.msg}>"
					});
					
		});

function loadData(page, pageSize) {
	var stcm_area = $('[name=stcm_area]').val();
	var st_code = $('[name=st_code]').val();
	var country_id = $('[name=country_id]').val();
	$.ajax({
		type : "POST",
		async : false,
		dataType : "json",
		url : "/shipping/ship-type-country-map/index",
		data : {
			'page' : page,
			'pageSize' : pageSize,
			'stcm_area' : stcm_area,
			'st_code' : st_code,
			'country_id' : country_id
		},
		success : function(json) {
			paginationTotal = json.count;
			var html = "";
			if (json.count == 0) {
				html = "<td colspan='5' class=''>&nbsp;<{$lang.no_data}></td>";
			} else {
				$.each(json.list, function(key, val) {
					html += "<tr>";
					html += "<td>" + val.country_name + "</td>";
					html += "<td>" + val.country_code + "</td>";
					html += "<td>" + val.stcm_area + "</td>";
					html += "<td>" + val.st_code + "</td>";
					html += "<td><a href=\"javascript:editPage('" + val.stcm_id
							+ "');\"><{$lang.edit}></a>&nbsp;&nbsp;<a href=\"javascript:void(0)\" onclick=\"alertInfo('"+ val.stcm_id + "')\"><{$lang.delete}></a></td>";
					html += "</tr>";
				})
			}
			$("#list").html(html);

		}
	});
}

function search() {
	initData(0);
}

function showEditPage(obj, titles, asc) {
	if (asc == 'add') {
		obj.dialog({
					title : titles,
					buttons : {
						'<{$lang.add}>' : function() {
							editSubmit()
						},
						'<{$lang.close}>' : function() {
							$(this).dialog('close');
						}
					}
				});
	} else {
		obj.dialog({
					title : titles,
					buttons : {
						'<{$lang.edit}>' : function() {
							editSubmit()
						},
						'<{$lang.close}>' : function() {
							$(this).dialog('close');
						}
					}
				});
	}
	obj.dialog("open");
}
function editPage(stcm_id) {
	if (stcm_id == 0) {
		$('#edit [name=country_id]').focus();
		$('#edit [name=stcm_id]').val(0);
		$('#editForm').resetForm();
		showEditPage($('#edit'), '<{$lang.add}><{$lang.shipping}><{$lang.zone}>', 'add');
		return;
	}
	
	$.ajax({
		type : "POST",
		async : false,
		dataType : "json",
		url : "/shipping/ship-type-country-map/info", // 请求的处理url
		data : {
			'stcm_id' : stcm_id
		},
		success : function(json) {
			if (!json.ask) {
				var html = '<p class="alertMsg"><img src="/images/not.gif"><{$lang.shipping}><{$lang.zone}><{$lang.msg}>获取<{$lang.fail}></p>';
				$('#message').html(html);
				$('.messageDiv').dialog("open");
				return false;
			}
			var row = json.list;
			$('#edit [name=stcm_id]').val(row.stcm_id);
			$('#edit [name=country_id]').val(row.country_id);
			$('#edit [name=st_code]').val(row.st_code);
			$('#edit [name=stcm_area]').val(row.stcm_area);

			showEditPage($('#edit'), '<{$lang.edit}><{$lang.shipping}><{$lang.zone}>', 'edit');
		}
	});
	
}

function editSubmit() {
	var formData = $('#editForm').serialize();
	$.ajax({
		type : "POST",
		async : false,
		dataType : "json",
		url : "/shipping/ship-type-country-map/edit", // 请求的处理url
		data : formData,
		success : function(json) {
			var html = "";
			if (json.ask) {
				var html = '<p class="alertMsg"><img src="/images/yes.gif">'
						+ json.list.message + '</p>';
				$('#message').html(html);
			} else {
				var error = json.list.error;
				$.each(error, function(key, val) {
							html += '<p class="alertMsg"><img src="/images/no.gif">'
									+ val + '</p>';
						});
				$('#message').html(html);
			}
			$('.messageDiv').dialog("open");
			var curPage = $(".pagination span> .current").html();
			initData(curPage);
		}
	});

}

function alertInfo(stcm_id) {
	$("#confrimDiv").dialog({
				buttons : {
					"<{$lang.confirm}>" : function() {
						del(stcm_id)
					},
					"<{$lang.return}>" : function() {
						$(this).dialog('close')
					}
				}
			});
	$('#confrimMsg').html("<{$lang.confirm}><{$lang.delete}><{$lang.this}><{$lang.data}>");
	$('#confrimDiv').dialog("open");
}
function del(stcm_id) {
	$('#confrimDiv').dialog("close");
	$.ajax({
		type : "POST",
		async : false,
		dataType : "json",
		url : "/shipping/ship-type-country-map/delete", // 请求的处理url
		data : {
			'stcm_id' : stcm_id
		},
		success : function(json) {
			var html = "";
			if (json.ask) {
				var html = '<p class="alertMsg"><img src="/images/yes.gif">'
						+ json.list.message + '</p>';
				$('#message').html(html);
			} else {
				var error = json.list.error;
				$.each(error, function(key, val) {
							html += '<p class="alertMsg"><img src="/images/no.gif">'
									+ val + '</p>';
						});
				$('#message').html(html);
			}
			$('.messageDiv').dialog("open");
			var curPage = $(".pagination span> .current").html();
			initData(curPage);
		}
	});
}

</script>
<style type="text/css">
.button{background:url(images/left-menu-li.gif) repeat-x left top #F1F6F9; border:1px solid #318DAC; font-weight:bold; padding:2px 5px; font-weight:lighter; color:#318DAC; vertical-align:middle; -moz-border-radius:3px; -webkit-border-radius:3px; margin:auto 5px}
</style>

<div id="iTitle">
	<div id="iTitleSub">
		<form id="searchForm" name="searchForm" >
		<div>
		<label>
		<{$lang.zone}><{$lang.label}>：
        <input  class="myInput" type="text" name="stcm_area">
		</label>&nbsp;
        <label>
		<{$lang.shipping_method}>：
        <select  name="st_code" class="ui-widget-content">
        <option value=""><{$lang.pls}></option>
        <{foreach item="shipType" from=$shipTypeList key='key'}>
        <option value="<{$shipType.st_code}>"><{$shipType.st_code}></option>
        <{/foreach}>
       </select>
		</label>&nbsp;
        <label>
		<{$lang.country}>：
       <select  name="country_id" class="ui-widget-content" style="width:100px">
        <option value=""><{$lang.pls}></option>
        <{foreach item="country" from=$countryList key='key'}>
        <option value="<{$country.country_id}>"><{$country.en_name}>(<{$country.cn_name}>)</option>
        <{/foreach}>
       </select>
     
	   </label>&nbsp;
		<input class="myButton" type="button" value="<{$lang.search}>" onclick="search();">&nbsp;&nbsp;
		<input class="myButton" type="button" value="<{$lang.add}>"  id="addButton" onclick="editPage(0)">&nbsp;&nbsp;
        
        <input class="myButton" type="button" value="<{$lang.batch}><{$lang.add}>"  id="addBatchButton" onclick="javascript:window.location='/shipping/ship-price/upload/type/ship_type_country_map'">&nbsp;&nbsp;
		</div>
		</form>
	</div>
	<div id="tipmsg"></div>

</div>
<div class="listDiv ui-widget-content"></div>
<h3><{$lang.shipping}><{$lang.zone}><{$lang.list}></h3>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="myTab">	
    <thead>
    <tr>
    <th><{$lang.country}></th>
    <th><{$lang.country}><{$lang.code}></th>
    <th><{$lang.zone}></th>
    <th><{$lang.shipping_method}></th>
    <th><{$lang.operation}></th>
    </tr>
    </thead>
	<tbody id="list"></tbody>
</table>
<div class="pagination"></div>
<div style="display:none;" class="messageDiv">
    <div id="message"></div>
</div>

<div style="display:none;" id="confrimDiv">
    <div id="confrimMsg"></div>
</div>

<div style="display:none;" id="edit" >
    <{include file='shipping/shiptypecountrymap/edit.tpl'}>
</div>